package com.mifengs.mall.widget.UploadPhotoView;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mifengs.mall.R;

/**
 * Created by lihaoqi on 2018/3/22.
 */

public class UpLoadPhotoView extends FrameLayout implements View.OnClickListener{

    public enum UpLoadStatus{
        NORMAL,
        SUCCESS,
        FAIL,
        LOADING
    }

    private UpLoadPhotoViewController mController;

    private ImageView mPhotoView;

    private FrameLayout mContainer;

    private OnUpLoadPhotoViewListener mResultListener;

    private OnClickUpViewListener mClickUpViewListener;

    private RelativeLayout mRlNormal;

    private FrameLayout mflOther;

    private LinearLayout mLlLoading;

    private LinearLayout mLlFail;

    private TextView tv_tip;

    public UpLoadPhotoView(@NonNull Context context) {
        this(context,null);
    }

    public UpLoadPhotoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public UpLoadPhotoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mController = new UpLoadPhotoViewController(this);
        mResultListener = mController;
        LayoutInflater.from(context).inflate(R.layout.upload_photo_view, this, true);
        tv_tip = findViewById(R.id.tv_tip);
        mPhotoView = findViewById(R.id.iv_photo);
        mContainer = findViewById(R.id.fl_container);

        mContainer.setOnClickListener(this);

        mRlNormal =  findViewById(R.id.rl_normal);
        mflOther = findViewById(R.id.fl_other);

        mLlLoading = findViewById(R.id.ll_loading);
        mLlFail = findViewById(R.id.ll_fail);

    }

    public void setText(String str){
        tv_tip.setText(str);
    }




    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    public OnUpLoadPhotoViewListener getResultListener() {
        return mResultListener;
    }

    public ImageView getPhotoView() {
        return mPhotoView;
    }

    @Override
    public void onClick(View v) {
        mController.choosePhoto();
        if (mClickUpViewListener!=null)
            mClickUpViewListener.onUpViewClick(this);
    }

    public void setNormalView(){
        mRlNormal.setVisibility(VISIBLE);
    }

    public UpLoadPhotoViewController getController() {
        return mController;
    }

    public void setSuccessView(){
        mRlNormal.setVisibility(GONE);
        mLlFail.setVisibility(GONE);
        mLlLoading.setVisibility(GONE);
    }

    public void setFailView(){
        mRlNormal.setVisibility(GONE);
        mLlFail.setVisibility(VISIBLE);
        mLlLoading.setVisibility(GONE);
    }

    public void setLoadingView(){
        mRlNormal.setVisibility(GONE);
        mLlFail.setVisibility(GONE);
        mLlLoading.setVisibility(VISIBLE);
    }

    public interface OnClickUpViewListener{
        void onUpViewClick(View view);
    }

    public void setClickUpViewListener(OnClickUpViewListener clickUpViewListener) {
        this.mClickUpViewListener = clickUpViewListener;
    }
}
